package com.neusoft.neuiotms.dcam.fixed.repository.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.fixed.model.InventoryResult;
import com.neusoft.neuiotms.dcam.fixed.model.QInventoryResult;
import com.neusoft.neuiotms.dcam.fixed.model.enums.InventoryDutyResultStatus;
import com.neusoft.neuiotms.dcam.fixed.model.enums.InventoryDutyStatus;
import com.neusoft.neuiotms.dcam.fixed.repository.CustomizedInventoryResultRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedInventoryResultRepositoryImpl extends AbstractRepository
        implements CustomizedInventoryResultRepository {

    @Override
    public Page<InventoryResult> search(final Pageable pageable, final Searchable searchable) {
        final QInventoryResult qInventoryResult =
                com.neusoft.neuiotms.dcam.fixed.model.QInventoryResult.inventoryResult;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<InventoryResult> query = this.query().selectFrom(qInventoryResult);
        query.where(where);
        return super.search(query, pageable);
    }

    @Override
    public List<InventoryResult> search(final Searchable searchable) {
        final QInventoryResult qInventoryResult =
                com.neusoft.neuiotms.dcam.fixed.model.QInventoryResult.inventoryResult;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<InventoryResult> query = this.query().selectFrom(qInventoryResult);
        query.where(where);
        return query.fetch();
    }

    @Override
    protected Class<?> getModelClass() {
        return InventoryResult.class;
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {
        final BooleanBuilder where = new BooleanBuilder();
        final QInventoryResult qInventoryResult =
                com.neusoft.neuiotms.dcam.fixed.model.QInventoryResult.inventoryResult;

        // 结果ID
        if (null != searchable.getLongValue("id")) {
            where.and(super.equalsLong(qInventoryResult.id, searchable, "id"));
        }

        // 结果IDS
        if (StringUtils.isNotEmpty(searchable.getStrValue("ids"))) {
            where.and(super.inLongs(qInventoryResult.id, searchable, "ids"));
        }

        // 任务ID
        if (StringUtils.isNotEmpty(searchable.getStrValue("dutyId"))) {
            where.and(super.equalsLong(qInventoryResult.inventoryDuty.id, searchable, "dutyId"));
        }

        // 状态
        if (!StringUtils.isEmpty(searchable.getStrValue("dutyResultStatus"))) {
            where.and(super.equalsEnum(qInventoryResult.dutyResultStatus, searchable, "dutyResultStatus",
                    value -> InventoryDutyResultStatus.valueOf(value)));
        }

        // 资产名称
        if (!StringUtils.isEmpty(searchable.getStrValue("name"))) {
            where.and(super.containsStr(qInventoryResult.name, searchable, "name"));
        }

        // 计划ID
        if (StringUtils.isNotEmpty(searchable.getStrValue("planId"))) {
            where.and(super.equalsLong(qInventoryResult.inventoryDuty.inventoryPlan.id, searchable, "planId"));
        }

        // 任务状态
        if (!StringUtils.isEmpty(searchable.getStrValue("dutyStatus"))) {
            where.and(super.equalsEnum(qInventoryResult.inventoryDuty.status, searchable, "dutyStatus",
                    value -> InventoryDutyStatus.valueOf(value)));
        }

        // 结果状态
        if (!StringUtils.isEmpty(searchable.getStrValue("status"))) {
            where.and(super.equalsEnum(qInventoryResult.dutyResultStatus, searchable, "status",
                    value -> InventoryDutyResultStatus.valueOf(value)));
        }

        return where;
    }

    @Override
    public List<InventoryResult> getByDutyIdAndCode(final long dutyId, final String code) {
        final QInventoryResult qInventoryResult =
                com.neusoft.neuiotms.dcam.fixed.model.QInventoryResult.inventoryResult;
        final JPAQuery<InventoryResult> query = this.query().selectFrom(qInventoryResult);
        query.where(qInventoryResult.inventoryDuty.id.eq(dutyId)).where(qInventoryResult.code.eq(code));
        return query.fetch();
    }
}
